//
//  CZQSearchView.m
//  WNZSSQ
//
//  Created by JDKJ on 2018/12/5.
//  Copyright © 2018年 JDKJ. All rights reserved.
//

#import "CZQSearchView.h"

@implementation CZQSearchView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        //self.size = CGSizeMake(300, 30);
        self.layer.cornerRadius = 20;
        self.backgroundColor = CSWhiteColor;
        
        UITextField *field = [UITextField new];
        field.font = MidFont;
        field.textColor = TFColor;
        field.placeholder = @"请输入你想看的书名\作者名\主角名";
        // 提前在Xcode上设置图片中间拉伸
        //        self.background = [UIImage imageNamed:@"searchbar_textfield_background"];
        field.attributedPlaceholder = [[NSAttributedString alloc] initWithString:field.placeholder attributes:@{NSForegroundColorAttributeName: CSColorFromRGB(0xDCDCDC)}];
        [self addSubview:field];
        self.seachField = field;
       field.returnKeyType = UIReturnKeySearch;
        
        // 通过init初始化的控件大多都没有尺寸
        UIButton *searchIcon = [[UIButton alloc] init];
        [searchIcon setImage:@"搜索框搜索图标"];
        
        // contentMode：default is UIViewContentModeScaleToFill，要设置为UIViewContentModeCenter：使图片居中，防止图片填充整个imageView
        searchIcon.contentMode = UIViewContentModeCenter;
        searchIcon.size = CGSizeMake(20, 20);
        field.clearButtonMode = UITextFieldViewModeWhileEditing;
        
        field.leftView = searchIcon;
        field.leftViewMode = UITextFieldViewModeAlways;
        
        [field mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(self).offset(0);
            make.left.mas_equalTo(self).offset(10);
            make.bottom.mas_equalTo(self).offset(-0);
            make.right.mas_equalTo(self).offset(-10);
        }];
        
    }
    return self;
}

+(instancetype)searchBar
{
    return [[self alloc] init];
}

@end
